import { Controller } from 'egg';
import { RESULT_CODE } from '../utils/constants/code';

export default class ExclusiveRestaurantInviteController extends Controller {
  // 根据手机号查找用户，并返回邀请状态
  async searchUserByMobile() {
    const { ctx } = this;
    const { restaurant_id, mobile } = ctx.query as any;
    if (!restaurant_id || !mobile) ctx.throw(RESULT_CODE.FAIL, 'restaurant_id 与 mobile 必填');

    try {
      const data = await ctx.service.exclusiveRestaurantInviteService.findUserByMobileWithInviteStatus({
        restaurant_id: Number(restaurant_id),
        mobile: String(mobile),
      });
      ctx.body = { code: RESULT_CODE.SUCCESS, message: '获取成功', data };
    } catch (error: any) {
      ctx.throw(RESULT_CODE.FAIL, error.message);
    }
  }
  // （1）我的邀请列表：type=sent|received
  async listInvites() {
    const { ctx } = this;
    const { type = 'received', page = 1, limit = 10 } = ctx.query as any;
    if (!['sent', 'received'].includes(type)) ctx.throw(RESULT_CODE.FAIL, 'type 不合法');

    try {
      const data = await ctx.service.exclusiveRestaurantInviteService.listInvites({
        type: type as any,
        page: Number(page),
        limit: Number(limit),
      });
      ctx.body = { code: RESULT_CODE.SUCCESS, message: '获取成功', data };
    } catch (error: any) {
      ctx.throw(RESULT_CODE.FAIL, error.message);
    }
  }

  // （2）我接受邀请的餐厅列表
  async listAcceptedRestaurants() {
    const { ctx } = this;
    const { page = 1, limit = 10 } = ctx.query as any;
    try {
      const data = await ctx.service.exclusiveRestaurantInviteService.listAcceptedRestaurants({
        page: Number(page),
        limit: Number(limit),
      });
      ctx.body = { code: RESULT_CODE.SUCCESS, message: '获取成功', data };
    } catch (error: any) {
      ctx.throw(RESULT_CODE.FAIL, error.message);
    }
  }

  // （3）发出邀请
  async sendInvite() {
    const { ctx } = this;
    const { restaurant_id, invitee_id, remark } = ctx.request.body || {};
    if (!restaurant_id || !invitee_id) ctx.throw(RESULT_CODE.FAIL, 'restaurant_id 与 invitee_id 必填');

    try {
      const row = await ctx.service.exclusiveRestaurantInviteService.sendInvite({
        restaurant_id: Number(restaurant_id),
        invitee_id: Number(invitee_id),
        remark,
      });
      ctx.body = { code: RESULT_CODE.SUCCESS, message: '邀请已发送', data: row };
    } catch (error: any) {
      ctx.throw(RESULT_CODE.FAIL, error.message);
    }
  }

  // （4）处理邀请：action=accept|reject|cancel
  async processInvite() {
    const { ctx } = this;
    const { id, action } = ctx.request.body || {};
    if (!id || !action) ctx.throw(RESULT_CODE.FAIL, 'id 与 action 必填');
    if (!['accept', 'reject', 'cancel'].includes(action)) ctx.throw(RESULT_CODE.FAIL, 'action 不合法');

    try {
      const ret = await ctx.service.exclusiveRestaurantInviteService.processInvite({
        id: Number(id),
        action,
      });
      ctx.body = { code: RESULT_CODE.SUCCESS, message: ret.message, data: null };
    } catch (error: any) {
      ctx.throw(RESULT_CODE.FAIL, error.message);
    }
  }
}